package cn.tedu.springmvc.controller;

import cn.tedu.springmvc.dao.HttpDao;
import cn.tedu.springmvc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HttpController {
    @Autowired
    HttpDao httpDao;

    //value属性表示访问路径,method表示请求方式
    @RequestMapping(value = "getUser",method = RequestMethod.GET)
    public String getUserAll() {
        List<User> list = httpDao.getUserAll();
        return list.toString();
    }

    @RequestMapping(value = "getUserById",method = RequestMethod.GET)
    public User getUserById(int id) {
        User user = httpDao.getUserById(id);
        return user;
    }

    @RequestMapping(value = "saveUser",method = RequestMethod.POST)
    //@RequestBody 从请求体中取值,不加则获取不到值
    public String postUser(@RequestBody User user) {
        httpDao.postUser(user);
        return "添加成功!";
    }

    @RequestMapping(value = "deleteUser",method = RequestMethod.DELETE)
    public String deleteUser(int id) {
        httpDao.deleteUser(id);
        return "删除成功!";
    }
}
